//
//  Search.swift
//  YoruNeko
//
//  Created by Apple on 2021/6/21.
//

import SwiftUI
#if canImport(UIKit)
extension View {
    func hideKeyboard() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}
#endif
struct Search: View {
    @EnvironmentObject var modelData:HistoryListModelData
    var currentPageUrl: String
    var currentPageTitle: String
    var currentPageLogo : Image
    var callBack:ViewCallBack?
    var body: some View {
        ZStack{
            Color.white
                .onTapGesture {
                    hideKeyboard()
                    modelData.isEditing = false
                }
            VStack{
                if modelData.Searchtext == "" {
                    CurrentPage(logo: currentPageLogo, url: currentPageUrl, title: currentPageTitle, callBack: callBack)
                        .transition(.move(edge: .trailing))
                        .animation(.default)
                    Divider()
                }else {
                    SearchItem(callBack: callBack)
                        .transition(.move(edge: .leading))
                        .animation(.default)
                }
                HistoryList(callBack: callBack)
                Spacer()
            }
        }
    }
}

struct Search_Previews: PreviewProvider {
    static var previews: some View {
        Search(currentPageUrl: "https://www.baidu.com", currentPageTitle:"当前标签页的title", currentPageLogo: Image("baidu"))
            .environmentObject(HistoryListModelData())
    }
}
